
import { BUTTONID, AREA, UISTATUS, FIELDS, CARDTABLEAREAIDS,CARDTABLEAREAIDS2, DELETELINEBTNS } from '../../constance';
import { clearCardData } from '../utils/cardPageUtil';

function buttonControl(props) {
        let status = props.getUrlParam('status'); // 页面状态

        // 1、设置页面状态
        setUIState(props, status);
        // 2、按钮状态控制
        setButtonState(props, status);
        // 3、主按钮设置
        // setMainButton(props, status);
        // 4、返回按钮控件状态控制
        setHeadInfoState(props, status);
        // 5、卡片分页器状态控制
        setCardPaginationState(props, status);
}

function setUIState(props, status) {
        props.form.setFormStatus(AREA.cardFormId, status == UISTATUS.copy ? UISTATUS.add : status);
        let uistatus = status == UISTATUS.browse ? status : UISTATUS.edit;
        CARDTABLEAREAIDS.forEach((areaId) => {
                props.cardTable.setStatus(areaId, uistatus);
        });
        CARDTABLEAREAIDS2.forEach((areaId) => {
                props.cardTable.setStatus(areaId, uistatus);
        });

        let isDisabled = status != UISTATUS.add;
        props.form.setFormItemsDisabled(AREA.cardFormId, { [FIELDS.pk_org]: isDisabled }); // 设置主组织编辑性
}

function setButtonState(props, status) {
        props.button.setButtonVisible(
                [
                        BUTTONID.Add,
                        BUTTONID.Edit,
                        BUTTONID.Delete,
                        BUTTONID.Copy,
                        BUTTONID.Refresh,
                        BUTTONID.ftDetail,
                        BUTTONID.RedHandle,
                        BUTTONID.Voucher,
                        BUTTONID.Commit
                ],
                status == UISTATUS.browse
        );

        props.button.setButtonVisible(
                [
                        BUTTONID.Save,
                        BUTTONID.Cancel,
                                        BUTTONID.cardBody_1_AddLine,
                        BUTTONID.cardBody_2_AddLine,
                BUTTONID.cardBody_1_DeleteLine,
                BUTTONID.cardBody_2_DeleteLine,
                BUTTONID.cardBody_1_CopyLine,
                BUTTONID.cardBody_2_CopyLine
                ],
                status != UISTATUS.browse
        );

        props.button.setButtonVisible(
                [
                        BUTTONID.cardBody_1_PasteLineToTail,
                        BUTTONID.cardBody_2_PasteLineToTail,
                        BUTTONID.cardBody_1_CancelB,
                        BUTTONID.cardBody_2_CancelB

                ],
                false
        );
        if(status == UISTATUS.browse){

                let hasBlue = false;//是否制单
                let hasRed = false;//是否关闭
                //已摊金额
                let hascount = props.form.getFormItemsValue([ AREA.cardFormId ], 'hascount') && props.form.getFormItemsValue([ AREA.cardFormId ], 'hascount').value;
                hasRed = props.form.getFormItemsValue([ AREA.cardFormId ], 'isflag') && props.form.getFormItemsValue([ AREA.cardFormId ], 'isflag').value;

                if(hascount){
                        let nhascount = parseFloat(hascount);
                        if(nhascount > 0){
                                hasBlue = true;
                        }
                }
                

                let approveStatus = props.form.getFormItemsValue([ AREA.cardFormId ], 'approvestatus').value;
                if (approveStatus == '-1') {
                        props.button.setButtonVisible([BUTTONID.Commit], true);
                        props.button.setButtonVisible([BUTTONID.UnCommit], false);
                } else if (approveStatus == '2' || approveStatus == '3') {
                        props.button.setButtonVisible([BUTTONID.Commit, BUTTONID.Edit, BUTTONID.Delete, BUTTONID.Copy], false);
                        props.button.setButtonVisible([BUTTONID.UnCommit], true);
                } else if(approveStatus == 1) {
                        props.button.setButtonVisible([BUTTONID.Commit, BUTTONID.Edit, BUTTONID.Delete, BUTTONID.Copy], false);
                        if(hasBlue || hasRed){
                                props.button.setButtonVisible([BUTTONID.UnCommit], false);
                        }else{
                                props.button.setButtonVisible([BUTTONID.UnCommit], true);
                        }
                        
                        
                }else {
                        props.button.setButtonVisible([BUTTONID.Commit, BUTTONID.UnCommit, BUTTONID.Edit, BUTTONID.Copy, BUTTONID.Delete], false);
                }
        }
        

        const scene = props.getUrlParam('scene');
        if (scene === 'approvesce') {
                props.button.setButtonVisible([ BUTTONID.Edit, BUTTONID.Refresh ], status == UISTATUS.browse);
                props.button.setButtonVisible([ BUTTONID.Save, BUTTONID.Refresh ], status != UISTATUS.browse);
                props.button.setButtonVisible([BUTTONID.Add, BUTTONID.UnCommit, BUTTONID.Commit, BUTTONID.Delete, BUTTONID.Copy], false);
        }

        let ishasRedPz =props.form.getFormItemsValue([ AREA.cardFormId ], 'isflag') && props.form.getFormItemsValue([ AREA.cardFormId ], 'isflag').value

        let btnNoShow = [];

        if(ishasRedPz){
                btnNoShow.push(BUTTONID.ftDetail, BUTTONID.Voucher, BUTTONID.RedHandle);
        }

        props.button.setButtonVisible(
                btnNoShow, false
        );
}

function setHeadInfoState(props, status) {
        props.BillHeadInfo.setBillHeadInfoVisible({
                showBackBtn: status == UISTATUS.browse,
                showBillCode: true,
                billCode: null
        });
        const scene = props.getUrlParam('scene');
        if (scene === 'approvesce') {
                props.BillHeadInfo.setBillHeadInfoVisible({
                        showBackBtn: false,
                        showBillCode: true,
                        billCode: null
                });
        }
}

function setCardPaginationState(props, status) {
        props.cardPagination.setCardPaginationVisible('cardPaginationBtn', status == UISTATUS.browse);
        const scene = props.getUrlParam('scene');
        if (scene === 'approvesce') {
                props.cardPagination.setCardPaginationVisible('cardPaginationBtn', false);
        }
}


/**
 * 设置卡片肩部按钮可用性：删行、复制行
 */
function onRowsSelected(props, moduleId) {
        let buttonArr = DELETELINEBTNS[moduleId];
        let checkedRows = props.cardTable.getCheckedRows(moduleId);
        if (checkedRows && checkedRows.length > 0) {
                props.button.setButtonDisabled(buttonArr, false);
        } else {
                props.button.setButtonDisabled(buttonArr, true);
        }
}

/**
 * 无数据时，设置卡片空界面
 * @param {*} props
 */
function setEmptyValueDisplay(props) {
        clearCardData(props); // 清空卡片数据
        setUIState(props, UISTATUS.browse); // 设置卡片编辑性

        props.BillHeadInfo.setBillHeadInfoVisible({
                showBackBtn: true,
                showBillCode: false
        });
        // 设置按钮可见性
        props.button.setButtonVisible([ BUTTONID.Add ], true);
        props.button.setButtonVisible(
                [
                        BUTTONID.Edit,
                        BUTTONID.Delete,
                        BUTTONID.Copy,
                        BUTTONID.Refresh,
                        BUTTONID.Save,
                        BUTTONID.Cancel,
                        BUTTONID.Commit,

                        BUTTONID.cardBody_1_AddLine,
                        BUTTONID.cardBody_2_AddLine,
                        BUTTONID.cardBody_1_DeleteLine,
                        BUTTONID.cardBody_2_DeleteLine,
                        BUTTONID.cardBody_1_CopyLine,
                        BUTTONID.cardBody_2_CopyLine,
                        BUTTONID.cardBody_1_PasteLineToTail,
                        BUTTONID.cardBody_2_PasteLineToTail,
                        BUTTONID.cardBody_1_CancelB,
                        BUTTONID.cardBody_2_CancelB
                ],
                false
        );
        setCardPaginationState(props, false);
}

export { buttonControl, onRowsSelected, setEmptyValueDisplay};
